package cn.sytton.taffe.web.sys.service.impl;

import cn.sytton.taffe.framework.base.service.BaseService;
import cn.sytton.taffe.web.sys.entity.Auth;
import cn.sytton.taffe.web.sys.entity.RoleMenuEntity;
import cn.sytton.taffe.web.sys.entity.RoleMenuFuncEntity;
import cn.sytton.taffe.web.sys.mapper.RoleMenuFuncMapper;
import cn.sytton.taffe.web.sys.mapper.RoleMenuMapper;
import cn.sytton.taffe.web.sys.service.IRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色菜单服务层
 *
 * @author skyrock
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class RoleMenuService extends BaseService<RoleMenuMapper, RoleMenuEntity> implements IRoleMenuService {

    private final RoleMenuFuncMapper roleMenuFuncMapper;

    @Override
    public Map<String, Object> findMenu(String roleId) {
        List<String> menuIds = list(new LambdaQueryWrapper<RoleMenuEntity>()
                .eq(RoleMenuEntity::getRoleId, roleId))
                .stream().map(RoleMenuEntity::getMenuId)
                .collect(Collectors.toList());
        List<String> funcKeys = roleMenuFuncMapper.selectList(new LambdaQueryWrapper<RoleMenuFuncEntity>()
                .eq(RoleMenuFuncEntity::getRoleId, roleId))
                .stream().map(f->f.getMenuId()+"#"+f.getFuncKey())
                .collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("menuIds", menuIds);
        result.put("funcKeys", funcKeys);

        return result;
    }

    @Override
    public void authMenu(Auth auth) {
        for(String menuIdFuncKey : auth.getDeleteMenuIdFuncKeys()) {
            if (StringUtils.hasText(menuIdFuncKey)) {
                String[] falseFuncKeyObject = menuIdFuncKey.split("#");
                roleMenuFuncMapper.delete(new LambdaQueryWrapper<RoleMenuFuncEntity>()
                        .eq(RoleMenuFuncEntity::getRoleId, auth.getRoleId())
                        .eq(RoleMenuFuncEntity::getMenuId, falseFuncKeyObject[0])
                        .eq(RoleMenuFuncEntity::getFuncKey, falseFuncKeyObject[1]));
            }
        }
        for (String menuId : auth.getDeleteMenuIds()) {
            if (StringUtils.hasText(menuId)) {
                remove(new LambdaQueryWrapper<RoleMenuEntity>()
                        .eq(RoleMenuEntity::getRoleId, auth.getRoleId())
                        .eq(RoleMenuEntity::getMenuId, menuId));
            }
        }
        for (String menuId : auth.getAddMenuIds()) {
            if (StringUtils.hasText(menuId)) {
                RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
                roleMenuEntity.setMenuId(menuId);
                roleMenuEntity.setRoleId(auth.getRoleId());
                baseMapper.create(roleMenuEntity);
            }
        }
        for (String menuIdFuncKey : auth.getAddMenuIdFuncKeys()) {
            if (StringUtils.hasText(menuIdFuncKey)) {
                String[] trueFuncKeyObject = menuIdFuncKey.split("#");
                String menuId = trueFuncKeyObject[0];
                String funcKey = trueFuncKeyObject[1];
                RoleMenuFuncEntity roleMenuFuncEntity = new RoleMenuFuncEntity();
                roleMenuFuncEntity.setMenuId(menuId);
                roleMenuFuncEntity.setFuncKey(funcKey);
                roleMenuFuncEntity.setRoleId(auth.getRoleId());
                roleMenuFuncMapper.create(roleMenuFuncEntity);
            }
        }
    }
}
